package com.bdzl.flow.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bdzl.flow.dao.ActNodeDao;
import com.bdzl.flow.service.ActNodeNextService;
import com.bdzl.flow.service.ActNodeRejectService;
import com.bdzl.flow.service.ActNodeService;
import com.bdzl.flow.servicefeign.AuthFeginService;
import com.bdzl.pub.entity.flow.ActNodeDO;
import com.bdzl.pub.entity.flow.ActNodeNextDO;
import com.bdzl.pub.entity.flow.ActNodeRejectDO;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.vo.biz.CheckVO;
import com.bdzl.pub.vo.flow.ActNodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class ActNodeServiceImpl implements ActNodeService {
	@Autowired
	private ActNodeDao actNodeDao;
	@Autowired
	private ActNodeNextService actNodeNextService;
	@Autowired
	private ActNodeRejectService actNodeRejectService;
	@Autowired
	private AuthFeginService authFeginService;

	@Override
	public ActNodeDO get(Integer nodeId){
		return actNodeDao.get(nodeId);
	}

	@Override
	public List<ActNodeVO> list(Map<String, Object> map){
		List<ActNodeVO> actNodeDOList = actNodeDao.list(map);
		System.out.println(actNodeDOList);
		actNodeDOList.forEach(actNodeDO -> {
			if ("user".equals(actNodeDO.getNodeUserType())){
				Object user = authFeginService.getUserById(Integer.valueOf(actNodeDO.getNodeUserVal()));
				HashMap result = (HashMap) user;
				actNodeDO.setNodeUserValName(String.valueOf(result.get("userNickname")));
				System.out.println("----------------"+user);
			}else {
				Object role = authFeginService.getRoleById(Integer.valueOf(actNodeDO.getNodeUserVal()));
				HashMap result = (HashMap) role;
				actNodeDO.setNodeUserValName(String.valueOf(result.get("roleName")));
				System.out.println("--------"+role);
			}

		});
		return actNodeDOList;
	}

	@Override
	public int count(Map<String, Object> map){
		return actNodeDao.count(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int save(ActNodeVO actNode){
		int save = actNodeDao.save(actNode);
		//获取下一步节点数据
		List<ActNodeNextDO> actNodeNextDOList = actNode.getActNodeNextDOList();
		if (actNodeNextDOList!=null&&actNodeNextDOList.size()>0){
			for (ActNodeNextDO actNodeNextDO : actNodeNextDOList) {
				actNodeNextDO.setAnnNodeNow(String.valueOf(actNode.getNodeId()));
				actNodeNextDO.setAnnCretime(new Date());
				actNodeNextService.save(actNodeNextDO);
			}
		}
		//获取驳回节点
		List<ActNodeRejectDO> actNodeRejectDOList = actNode.getActNodeRejectDOList();
		if (actNodeRejectDOList!=null&&actNodeRejectDOList.size()>0){
			for (ActNodeRejectDO actNodeRejectDO : actNodeRejectDOList) {
				actNodeRejectDO.setAnrCretime(new Date());
				actNodeRejectDO.setAnrNodeNow(String.valueOf(actNode.getNodeId()));
				actNodeRejectService.save(actNodeRejectDO);
			}
		}
		return save;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(ActNodeVO actNode){
		//获取下一节点数据
		List<ActNodeNextDO> actNodeNextDOList = actNode.getActNodeNextDOList();
		if (actNodeNextDOList != null && actNodeNextDOList.size()>0){
			actNodeNextDOList.forEach(actNodeNextDO -> {
				if (actNodeNextDO.getAnnId()!=null){
					actNodeNextService.update(actNodeNextDO);
				}else {
					actNodeNextDO.setAnnNodeNow(String.valueOf(actNode.getNodeId()));
					actNodeNextDO.setAnnCretime(new Date());
					actNodeNextService.save(actNodeNextDO);
				}
			});
		}
		//获取驳回节点数据
		List<ActNodeRejectDO> actNodeRejectDOList = actNode.getActNodeRejectDOList();
		if (actNodeRejectDOList != null && actNodeRejectDOList.size()>0){
			actNodeRejectDOList.forEach(actNodeRejectDO -> {
				if (actNodeRejectDO.getAnrId()!=null){
					actNodeRejectService.update(actNodeRejectDO);
				}else {
					actNodeRejectDO.setAnrCretime(new Date());
					actNodeRejectDO.setAnrNodeNow(String.valueOf(actNode.getNodeId()));
					actNodeRejectService.save(actNodeRejectDO);
				}
			});
		}
		return actNodeDao.update(actNode);
	}

	@Override
	public int remove(Integer nodeId){
		return actNodeDao.remove(nodeId);
	}

	@Override
	public int batchRemove(Integer[] nodeIds){
		return actNodeDao.batchRemove(nodeIds);
	}

	@Override
	public ActNodeVO getProcess(Integer nodeId) {
		ActNodeVO actNodeVO = actNodeDao.getProcess(nodeId);
		//查询执行人
		if ("user".equals(actNodeVO.getNodeUserType())){
			Object user = authFeginService.getUserById(Integer.valueOf(actNodeVO.getNodeUserVal()));
			HashMap result = (HashMap) user;
			actNodeVO.setNodeUserValName(String.valueOf(result.get("userNickname")));
			System.out.println("----------------"+user);
		}else {
			Object role = authFeginService.getRoleById(Integer.valueOf(actNodeVO.getNodeUserVal()));
			HashMap result = (HashMap) role;
			actNodeVO.setNodeUserValName(String.valueOf(result.get("roleName")));
			System.out.println("--------"+role);
		}
		return actNodeVO;
	}

	@Override
	public List<ActNodeVO> getNode(String nodeActKey) {
		return actNodeDao.getNode(nodeActKey);
	}

	@Override
	public ActNodeVO getNextNodeByNow(Integer annNodeNow, String annFormula) {
		return actNodeDao.getNextNodeByNow(annNodeNow,annFormula);
	}

	@Override
	public CheckVO getNodeByacrOdr(String acrOdr) {
		CheckVO checkVO = new CheckVO();
		//下一步节点数据
		List<ActNodeVO> nextNode = getNextNode(acrOdr);
		checkVO.setActNodeVOListY(nextNode);
		//驳回节点数据
		List<ActNodeVO> rejectNode = getRejectNode(acrOdr);
		checkVO.setActNodeVOListB(rejectNode);
		return checkVO;
	}



	/**
	* @Description: TODO 根据订单号查询下一步审核节点数据
	* @param acrOdr 订单号
	* @return: java.util.List<com.bdzl.pub.vo.flow.ActNodeVO>
	* @Author: Ming
	* @Date: 2021/5/18 18:59
	*/
	public List<ActNodeVO> getNextNode(String acrOdr){
		return actNodeDao.getNextNode(acrOdr);
	}

	/**
	 * @Description: TODO 根据订单号查询驳回审核节点数据
	 * @param acrOdr 订单号
	 * @return: java.util.List<com.bdzl.pub.vo.flow.ActNodeVO>
	 * @Author: Ming
	 * @Date: 2021/5/18 18:59
	 */
	public List<ActNodeVO> getRejectNode(String acrOdr){
		return actNodeDao.getRejectNode(acrOdr);
	}


	@Override
	public List<ActNodeVO> getOdrStatusByStatVal(String statVal) {
		return actNodeDao.getOdrStatusByStatVal(statVal);
	}

	@Override
	public UserDO getUserInfo(HttpServletRequest request){
		String accessToken = request.getHeader("X-Access-Token");
		Object user = authFeginService.getUser(accessToken);
		// 将数据转成json字符串
		String jsonObject= JSON.toJSONString(user);
		//将json转成需要的对象
		return JSONObject.parseObject(jsonObject,UserDO.class);
	}
}
